#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QTcpSocket>
#include <receivefile.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_s = new QTcpServer(this);
    ui->port->setText("8989");
    setWindowTitle("服务器");
    connect(m_s ,&QTcpServer::newConnection , this ,[=](){
        QTcpSocket* tcp = m_s->nextPendingConnection();
        //创建子线程
        ReceiveFile* sub_thread = new ReceiveFile(tcp);
        sub_thread->start();

        connect(sub_thread , &ReceiveFile::over , this , [=](){
            sub_thread->exit();
            sub_thread->wait();
            sub_thread->deleteLater();
            QMessageBox::information(this , "文件接受", "文件接受完成");
        });
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_set_listen_clicked()
{
    unsigned short port = ui->port->text().toUShort();
    m_s->listen(QHostAddress::Any,port);
}

